package com.hilihili.util;

import cn.hutool.core.date.DateUtil;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author xhc
 * @Date 2023/11/22 13:53
 * @Version 1.0
 */
public class DateChangeUtil {
    private DateChangeUtil(){}

    /**
     * 首页视频的日期格式
     * @param date
     * @return
     */
    public static String dateChange(Date date){
        // 现在的时间
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());					//放入Date类型数据
        // 视频上传的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (today.get(Calendar.YEAR)-calendar.get(Calendar.YEAR) > 0){
            //大于一年前的视频 年 月-日（month参数较为特殊，0表示1月）
            return calendar.get(Calendar.YEAR)+" "+(1+calendar.get(Calendar.MONTH))+"-"+calendar.get(Calendar.DATE);
        }else if(today.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)&&today.get(Calendar.DATE) == calendar.get(Calendar.DATE)){
            // 为同一天 xx小时前
            return today.get(Calendar.HOUR_OF_DAY) - calendar.get(Calendar.HOUR_OF_DAY) + "小时前";
        }else {
            // 今年的视频 月-日
            return (1+calendar.get(Calendar.MONTH))+"-"+calendar.get(Calendar.DATE);
        }
    }


    /**
     * 评论区的日期格式
     * @param date
     * @return
     */
    public static String commentDateChange(Date date){
        // 现在的时间
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());					//放入Date类型数据
        // 视频上传的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(today.get(Calendar.YEAR)==calendar.get(Calendar.YEAR)&&today.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)&&today.get(Calendar.DATE) == calendar.get(Calendar.DATE)){
            // 为同一天 xx小时前
            if (today.get(Calendar.HOUR_OF_DAY) - calendar.get(Calendar.HOUR_OF_DAY) == 0){
                // 同一小时内
                return today.get(Calendar.MINUTE) - calendar.get(Calendar.MINUTE) + "分钟前";
            }else {
                // 24小时制
                return today.get(Calendar.HOUR_OF_DAY) - calendar.get(Calendar.HOUR_OF_DAY) + "小时前";
            }
        }else {
            // YYYY-MM-DD HH:MM
            return calendar.get(Calendar.YEAR)+"-"+(1+calendar.get(Calendar.MONTH))+"-"+calendar.get(Calendar.DATE)
                    + " " + calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE);
        }
    }
    /**
     * 将秒数转化为 00：00：00的格式
     * 如果没有超过一小时则转转化为00：00
     * @param time
     * @return {@link String}
     */
    public static String formatDate(Integer time) {
        if (time/3600 == 0){
            String s = DateUtil.secondToTime(time);
            String substring = s.substring(3);
            return substring;
        }
        return DateUtil.secondToTime(time);
    }

    /**
     * 将日期类转换成 YYYY-MM-DD或MM-DD
     * @param date
     * @return
     */
    public static String formatDateGetYear(Date date){
        if (date == null) return "";
        // 现在的时间
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 判断是不是同一年
        if (today.get(Calendar.YEAR)==calendar.get(Calendar.YEAR)){
            // MM-DD
            return (1+calendar.get(Calendar.MONTH))+"-"+calendar.get(Calendar.DATE);
        }else {
            // YYYY-MM-DD
            return calendar.get(Calendar.YEAR)+"-"+(1+calendar.get(Calendar.MONTH))+"-"+calendar.get(Calendar.DATE);
        }
    }
    public static String getCurrentDateTime(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 定义输出的时间格式
        DateTimeFormatter outputDateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");

        // 格式化LocalDate对象为指定格式的字符串
        return currentDate.format(outputDateFormat);
    }
}
